package com.efast.dmom.mes.datahandlers.entity;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.efast.cafe.framework.util.SpringWebUtils;
import com.efast.dmom.mes.bean.Printer;
import com.efast.dmom.mes.bean.ResultBean;
import com.efast.dmom.mes.dao.entity.PrinterDao;
import com.efast.dmom.mes.datahandlers.base.PrintDataHandler;
import com.efast.dmom.mes.entity.PrinterEntity;
import com.efast.dmom.mes.util.TimeUtil;

@Component
public class PrinterDataHandler extends PrintDataHandler<Printer>{
	@Autowired
	private PrinterDao printerDao;
	
	
	@Override
	public ResultBean save(Printer obj) {
		ResultBean resultBean = new ResultBean();
		if (obj.getInnerData() != null) {
			obj.getInnerData().setSiteId(DataHandlerUtils.getSiteId());
			obj.getInnerData().setCreationTime(TimeUtil.getLocalDateTime());
			obj.getInnerData().setCreationTimeZone(TimeUtil.getLocalTimeZone());
			obj.getInnerData().setCreator(SpringWebUtils.getRequestUser());
			obj.getInnerData().setLastModifiedTime(TimeUtil.getLocalDateTime());
			obj.getInnerData().setLastModifiedTimeZone(TimeUtil.getLocalTimeZone());
			obj.getInnerData().setLastModifier(SpringWebUtils.getRequestUser());
//			obj.getInnerData().setCreationPid(DataHandlerUtils.getDmomPid());
//			obj.getInnerData().setLastModifiedPid(DataHandlerUtils.getDmomPid());
			obj.getInnerData().setRowStatus(1);
			obj.getInnerData().setRowTrxId(1);
			printerDao.save(obj.getInnerData());
		}
		return resultBean;
	}
	
	@Override
	public ResultBean update(Printer obj) {
		ResultBean resultBean = new ResultBean();
		if (obj.getInnerData() != null) {
			obj.getInnerData().setLastModifiedTime(TimeUtil.getLocalDateTime());
			obj.getInnerData().setLastModifier(SpringWebUtils.getRequestUser());
			obj.getInnerData().setLastModifiedTimeZone(TimeUtil.getLocalTimeZone());
//			obj.getInnerData().setLastModifiedPid(DataHandlerUtils.getDmomPid());		
			printerDao.update(obj.getInnerData());
		}
		return resultBean;
	}
	
	@Override
	public ResultBean delete(Printer obj) {
		ResultBean resultBean = new ResultBean();
		if (obj.getInnerData() != null) {
			obj.getInnerData().setLastModifiedTime(TimeUtil.getLocalDateTime());
			obj.getInnerData().setLastModifier(SpringWebUtils.getRequestUser());
			obj.getInnerData().setLastModifiedTimeZone(TimeUtil.getLocalTimeZone());
//			obj.getInnerData().setLastModifiedPid(DataHandlerUtils.getDmomPid());		
			printerDao.delete(obj.getInnerData());
		}
		return resultBean;
	}
   
	public Printer getByNameAndPlantId(String printerName,long plantId) {
		Printer printer = new Printer();
		PrinterEntity printerEntity = printerDao.getByNameAndPlantId(printerName, plantId);
		if(printerEntity!=null) {
			printer.setInnerData(printerEntity);
		}
		return printer;	
	}
	
	public Printer getById(long printerId) {
		Printer printer = new Printer();
		PrinterEntity printerEntity = printerDao.getById(printerId);
		if(printerEntity!=null) {
			printer.setInnerData(printerEntity);
		}
		return printer;	
	}
	
	public void batchDelete(List<Printer> listPrinter) {
		ArrayList<PrinterEntity> lists = new ArrayList<PrinterEntity>();
		if(listPrinter!=null && listPrinter.size()>0) {
			for(Printer list:listPrinter) {
				if(list.getInnerData()!=null) {
					list.getInnerData().setLastModifiedTime(TimeUtil.getLocalDateTime());
					list.getInnerData().setLastModifier(SpringWebUtils.getRequestUser());
					list.getInnerData().setLastModifiedTimeZone(TimeUtil.getLocalTimeZone());
//					obj.getInnerData().setLastModifiedPid(DataHandlerUtils.getDmomPid());
					lists.add(list.getInnerData());
				}
			}
			printerDao.batchDelete(lists);
		}
	}

	@Override
	public ResultBean saveBatch(List<Printer> obj) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public ResultBean updateBatch(List<Printer> obj) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public ResultBean deleteBatch(List<Printer> obj) {
		// TODO Auto-generated method stub
		return null;
	}
}
